#include "Config/Config.h"
#include "Log/Log.h"
#include "Net/TcpClient.h"
#include "Net/TcpServer.h"
#include "Net/UdpServer.h"
#include "Poco/Thread.h"
#include "UI.h"
#include "version.h"

#include <sys/wait.h>
#include <unistd.h>
#include "Json/JsonPath/JsonPath.h"

void InitApi();

void StartUIProcessor()
{
    try
    {
        UIProcessor* puiProcess = new UIProcessor;
        UdpServer* pus = new UdpServer(6001, puiProcess);
    }
    catch (...)
    {
        zlog.SError("%s catched\n", __PRETTY_FUNCTION__);
    }
}

int InitAll()
{
    ConfigManager::Instance();
    Config* config = ConfigManager::Instance()->GetConfig(CONFIG_FILE);
    if (config == NULL)
    {
        zlog.SError("config is NULL, not find file [%s] or it isn't a json file\n", CONFIG_FILE);
        return -1;
    }
    config->Start();

    zlog.InitFromConfig();
    zlog.Start();
    return 0;
}

int g_iStartTimes = 1;

void Deamon()
{
    pid_t iPid = 1;
    while (iPid > 0)
    {
        iPid = fork();
        if (iPid == -1)
        {
            zlog.SError("fork failed\n");
            return;
        }
        else if (iPid > 0)
        {
            wait(NULL);
            sleep(3);
            g_iStartTimes++;
        }
    }
}

int main(int argc, char** argv)
{

    bool bDeamon = true;
    if (bDeamon)
    {
        Deamon();
    }

    //start processor

    //init all
    if (InitAll() != 0)
    {
        return -1;
    }

    //loop
    while (1)
    {
        Poco::Thread::sleep(1000);
    }
    return 0;
}
